<?php

/**
 * 
 * @author truonghm
 * @date 06/04/2011
 * dynamic setup extjs form 
 *
 */

class Ofx_Controller_ExtjsForm  {
	
	/**
	 * 
	 * @var String $_form gia tri form duoc cau hinh
	 */
	private $_formPanel;
	
		
	function __construct() {		
		//if(!$p_formKey) return;		
		//$this->_formPanel = "";
		//echo "--<br>".$this->_formPanel;
	}	
	
	
	
	/**
	 * @param $p_formKey code form dinh nghia trong data
	 * Khoi tao va dinh nghia thanh phan tren form
	 * Thong tin cau hinh form duoc dinh nghia trong bang form va form_config
	 */	
	public function createFrom($p_formKey,$p_formId=null){
		//lay thong tin config trong data
		$cls_form = new Models_Form();
		
		$json = $cls_form->getColumnName($p_formKey);
		
		if($p_formId==null) $p_formId = $p_formKey;
		
		$this->_formPanel = new PhpExt_Form_FormPanel();
		$this->_formPanel->setId($p_formId)
						->setWidth('auto')
						->setBodyStyle("padding: 10px;")
						->setLabelAlign(PhpExt_Ext::HALIGN_RIGHT)
						->setDefaultType("textfield")
						->setBorder(false);;
		//set item to form
		$this->setItem($json);
		//echo $json;
		return $this->_formPanel;
	}
	
	
	/**
	 * @author truonghm
	 * @date 06/04/2011
	 * @desc set form item	 
	 */
	private function setItem($p_json){
		
		$arr = Zend_Json::decode($p_json,Zend_Json::TYPE_ARRAY);
		//print_r($arr);
		if(!$arr) return;
		
		$translate=Zend_Registry::get('translate');
		
		
		foreach($arr as $key=>$value){
			$vn_hidden = $value["hidden"];
			if($vn_hidden==1)
				$this->_formPanel->addItem(
						PhpExt_Form_Hidden::createHidden($value["name"])
					);
			else {
				if($value["type"]=='password')
					$obj_textField = PhpExt_Form_PasswordField::createPasswordField(
						 $value["name"]
						,$translate->translate($value["lable"])
					)->setAllowBlank($value["blank"]>0);
				elseif ($value["type"]=='editor')
					$obj_textField = PhpExt_Form_HtmlEditor::createHtmlEditor(
						 $value["name"]
						,$translate->translate($value["lable"])
					)->setEnableLinks(false)
					 ->setEnableSourceEdit(false)
					 ->setAutoHeight(true);
				else{						
					$obj_textField = PhpExt_Form_TextField::createTextField(
						 $value["name"]
						,$translate->translate($value["lable"])
						,null
						,$value["type"]
					)->setAllowBlank($value["blank"]>0);
				}				
				
				$this->_formPanel->addItem($obj_textField,new PhpExt_Layout_AnchorLayoutData("90%"));
			}
		}
	}	
	
	
	/**
	 * 
	 * @param string $p_formKey: code form
	 * @desc lay thong tin nguoi su dung nhap tren form
	 * @return object 
	 * {
	 * 	columns: [columnName: value input,...] // ten column va gia tri nhap vao trong bang chinh can ghi du lieu
	 *  exception: [columnName: value input,...]// ten column va gia tri ngoai le tren form
	 * }
	 */
	public function getInputValue($p_formKey)
	{
		$request = new Zend_Controller_Request_Http();
		$arr_valueInput =  $request->getParams();
		
		$cls = new Models_Form();
		$json = $cls->getColumnName($p_formKey);
		
		$arr_columnConfig = Zend_Json::decode($json,Zend_Json::TYPE_ARRAY);
		
		foreach($arr_columnConfig as $key=>$value){
			$arr_config[$value["name"]]=$key;
			$table_name = $value["table_name"];
		}
		
		//ten models class tuong ung
		$modelClassName = "Models_".strtoupper(substr($table_name, 0, 1)).substr($table_name, 1);
		
		$cls = new $modelClassName();
		
		//get all fields trong bang chinh
		$arr_columnsTable = $cls->info("cols");	
		foreach ($arr_columnsTable as $key=>$value)
			$arr_columns[$value] = $key;			
			
		//loc du lieu dua vao 2 mang $columns va exception
		$columns = array();
		$fix = array();
		$exception = array();
		
		foreach ($arr_valueInput as $key=>$value)
		{
			$columnName = $key;
			$columnValue = $value;
			if(isset($arr_columns[$columnName])){
				$columns[$key] = $value; //column trong bang chinh
				if(!isset($arr_config[$columnName])) 
					$fix[$key] = $value;// colums trong co trong bang chinh nhung khong co trong dinh nghia => fix tren form
			}
			else $exception[$key] = $value;//colums ngoai le => nhung colums nay nam o bang khac hoac su dung voi muc dinh rieng
		}
		
		$arr["columns"] = $columns;
		$arr["exception"] = $exception; 
		$arr["fix"] = $fix;	
			
		return Zend_Json::encode($arr);
	}	

}


?>